# encoding: utf-8

import json
from glob import glob
from tqdm import tqdm

txt_path = "./datas/export/*.txt"

file_list = glob(txt_path)

result = []

# 分别是：日期，开盘价，最高价，最低价，收盘价，成交量，成交额
for file in tqdm(file_list):
    with open(file, "r", encoding="gbk") as f:
        lines = f.readlines()
    stock_prices = []
    lines = [x.strip() for x in lines]
    # print(lines[0])
    for line in lines:
        split_texts = line.split(",")
        if len(split_texts) < 5:
            continue
        final_price = split_texts[4]
        stock_prices.append(float(final_price))

    # for start in range(0, len(stock_prices) - 11):
    #     result.append(stock_prices[start:start + 11])
    for idx in range(0, len(stock_prices)-11):
        current_price_list = stock_prices[idx:idx+10]
        final_price = stock_prices[idx+11]
        last_day_price = current_price_list[-1]
        change_price = final_price - last_day_price
        current_price_list.append(round(change_price, 2))
        result.append(current_price_list)


with open("./train.txt", "w", encoding="utf-8") as f:
    for row in result:
        f.write(json.dumps(row)+"\n")

